package com.hf.lookatme.zview.activitys;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hf.lookatme.R;
import com.hf.lookatme.commons.CommonMethod;
import com.hf.lookatme.customview.HeaderView;
import com.hf.lookatme.zview.fragments.Fragment_Main;
import com.hf.lookatme.zview.fragments.Fragment_Me;
import com.hf.lookatme.zview.fragments.Fragment_Social;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class Activity_MainPage extends Activity_Base {

  private static String TAG = "Activity_MainPage";

  @InjectView(R.id.headView_mainPage)
  HeaderView headerView;

  @InjectView(R.id.imageView_main_tab_me)
  ImageView imageView_tab_me;

  @InjectView(R.id.imageView_main_tab_social)
  ImageView imageView_tab_social;

  @InjectView(R.id.imageView_main_tab_mainPage)
  ImageView imageView_tab_main;

  @InjectView(R.id.textView_mainPage_tab_me)
  TextView textView_tab_me;

  @InjectView(R.id.textView_mainPage_tab_social)
  TextView textView_tab_social;

  @InjectView(R.id.textView_mainPage_tab_main)
  TextView textView_tab_main;

  private FragmentManager fragmentManager;

  private Fragment_Me fragment_me;

  private Fragment_Main fragment_main;

  private Fragment_Social fragment_social;

  private int currentIndex = 0;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    try {

      //添加此activity
      getApplicationMine().addActivityToAllActivity(this);
      //设置headerView
      headerView.setTitle(R.string.mainPage);
      //初始化Fragment
      initDefaultFragments();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 生成Fragment_Main
   */
  private void initDefaultFragments() {

    textView_tab_main.setSelected(true);
    imageView_tab_main.setSelected(true);

    fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment_Main fragment_main = new Fragment_Main();
    fragmentTransaction.replace(R.id.frameLayout_mainPage_fragmentSpace, fragment_main);
    fragmentTransaction.commit();
  }

  /**
   * 重置tab的选择状态
   */
  private void resetTabsSelected() {

    textView_tab_me.setSelected(false);
    imageView_tab_me.setSelected(false);
    textView_tab_main.setSelected(false);
    imageView_tab_main.setSelected(false);
    textView_tab_social.setSelected(false);
    imageView_tab_social.setSelected(false);
  }

  @OnClick(R.id.linearLayout_mainPage_tab_main)
  void onLayout_tab_mainClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      if (currentIndex == 0) {
        return;
      }
      resetTabsSelected();
      textView_tab_main.setSelected(true);
      imageView_tab_main.setSelected(true);

      if (fragmentManager == null) {
        fragmentManager = getFragmentManager();
      }
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
      if (fragment_main == null) {

        fragment_main = new Fragment_Main();
      }
      fragmentTransaction.replace(R.id.frameLayout_mainPage_fragmentSpace, fragment_main);
      fragmentTransaction.commit();
      currentIndex = 0;
    }
  }

  @OnClick(R.id.linearLayout_mainPage_tab_social)
  void onLayout_tab_socialClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      if (currentIndex == 1) {
        return;
      }
      resetTabsSelected();
      textView_tab_social.setSelected(true);
      imageView_tab_social.setSelected(true);

      if (fragmentManager == null) {
        fragmentManager = getFragmentManager();
      }
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
      if (fragment_social == null) {

        fragment_social = new Fragment_Social();
      }
      fragmentTransaction.replace(R.id.frameLayout_mainPage_fragmentSpace, fragment_social);
      fragmentTransaction.commit();
      currentIndex = 1;
    }
  }

  @OnClick(R.id.linearLayout_mainPage_tab_me)
  void onLayout_tab_meClicked(View view) {
    if (!CommonMethod.isFastDoubleClick()) {

      if (currentIndex == 2) {
        return;
      }
      resetTabsSelected();
      textView_tab_me.setSelected(true);
      imageView_tab_me.setSelected(true);

      if (fragmentManager == null) {
        fragmentManager = getFragmentManager();
      }
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
      if (fragment_me == null) {

        fragment_me = new Fragment_Me();
      }
      fragmentTransaction.replace(R.id.frameLayout_mainPage_fragmentSpace, fragment_me);
      fragmentTransaction.commit();
      currentIndex = 2;
    }
  }
}